package com.glodon.paas.monitor.server.biz.service.impl;

import java.util.Collections;
import java.util.List;

import javax.jms.JMSException;
import javax.jms.TextMessage;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.glodon.paas.job.model.MonitorEvent;
import com.glodon.paas.job.util.JsonUtil;
import com.glodon.paas.monitor.server.biz.domain.MonitorStatusEntity;
import com.glodon.paas.monitor.server.biz.mapper.MonitorStatusMapper;
import com.glodon.paas.monitor.server.biz.service.MonitorStatusService;
import com.glodon.paas.monitor.server.dto.MonitorStatusQueryDTO;

@Service("monitorStatusService")
public class MonitorStatusServiceImpl implements MonitorStatusService{

	@Autowired
	private MonitorStatusMapper	monitorStatusMapper;

	public void processMesage(TextMessage textMessage) throws JMSException {
		String json = textMessage.getText();
		MonitorEvent monitorEvent = JsonUtil.jsonToObject(json, MonitorEvent.class);
		processMonitorEvent(monitorEvent);
	}

	private void processMonitorEvent(MonitorEvent monitorEvent) {
		String traceId = monitorEvent.getTraceId();
		String type = monitorEvent.getType();
		MonitorStatusEntity monitorStatus = null;
		MonitorStatusQueryDTO monitorStatusQueryDTO=new MonitorStatusQueryDTO();
		monitorStatusQueryDTO.setMonitorType(type);
		monitorStatusQueryDTO.setTraceId(traceId);
		if (monitorEvent.isTraceIndex()) {
			List<MonitorStatusEntity> monitorStatusList = monitorStatusMapper.getByMonitorStatusQueryDTO(monitorStatusQueryDTO);
			int index = monitorStatusList.size() + 1;
			monitorStatus = new MonitorStatusEntity();
			monitorStatus.setTraceIndex(index);
			monitorStatus.setCreatedTime(monitorEvent.getTime());
		} else {
			monitorStatusQueryDTO.setTraceIndexFlag(true);
			List<MonitorStatusEntity> monitorStatusList = monitorStatusMapper.getByMonitorStatusQueryDTO(monitorStatusQueryDTO);
			if(null!=monitorStatusList && !monitorStatusList.isEmpty()){
				monitorStatus=(MonitorStatusEntity)monitorStatusList.get(0);
			}
			if (monitorStatus == null) {
				monitorStatus = new MonitorStatusEntity();
				monitorStatus.setCreatedTime(monitorEvent.getTime());
			} else {
				if (monitorEvent.isStartUp()) {
					monitorStatus.setCreatedTime(monitorEvent.getTime());
				}
			}
		}
		String taskId = (String) monitorEvent.getValues().get("taskId");
		monitorStatus.setTaskId(taskId);
		monitorStatus.setUpdatedTime(monitorEvent.getTime());
		monitorStatus.setMonitorType(type);
		monitorStatus.setCmd(monitorEvent.getCmd());
		monitorStatus.setTraceId(traceId);
		monitorStatus.getValues().putAll(monitorEvent.getValues());
		if (null == monitorStatus.getId()) {
			monitorStatusMapper.insertMonitorStatus(monitorStatus);
		} else {
			monitorStatusMapper.updateMonitorStatus(monitorStatus);
		}
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<MonitorStatusEntity> getByMonitorStatusQueryDTO(MonitorStatusQueryDTO monitorStatusQueryDTO) {
		if(null==monitorStatusQueryDTO){
			return Collections.EMPTY_LIST;
		}
		return monitorStatusMapper.getByMonitorStatusQueryDTO(monitorStatusQueryDTO);
	}

	@Override
	public void insertMonitorStatus(MonitorStatusEntity monitorStatus) {
		monitorStatusMapper.insertMonitorStatus(monitorStatus);
	}

	@Override
	public void updateMonitorStatus(MonitorStatusEntity monitorStatus) {
		monitorStatusMapper.updateMonitorStatus(monitorStatus);
	}
}
